package org.jetnuke.dao.generic;

import org.apache.log4j.Logger;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

import net.jforum.JForumExecutionContext;
import net.jforum.util.preferences.SystemGlobals;

import org.jetnuke.entities.Censore;

public class GenericCensoreDAO implements org.jetnuke.dao.CensoreDAO {
	/**
	 * Logger for this class
	 */
	private static final Logger logger = Logger
			.getLogger(GenericCensoreDAO.class);

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.jetnuke.model.CensoreModel#selectAll()
	 */
	public List selectAll() throws Exception {
		if (logger.isDebugEnabled()) {
			logger.debug("selectAll() - start"); //$NON-NLS-1$
		}

		PreparedStatement p = JForumExecutionContext.getConnection()
				.prepareStatement(
						SystemGlobals.getSql("CensoreModel.selectAll"));
		ResultSet rs = p.executeQuery();
		List list = this.processSelectAll(rs);
		rs.close();
		p.close();

		if (logger.isDebugEnabled()) {
			logger.debug("selectAll() - end"); //$NON-NLS-1$
		}
		return list;
	}

	/**
	 * @param rs
	 * @return
	 */
	protected List processSelectAll(ResultSet rs) throws Exception {
		if (logger.isDebugEnabled()) {
			logger.debug("processSelectAll(ResultSet) - start"); //$NON-NLS-1$
		}

		List list = new ArrayList();
		while (rs.next()) {
			Censore u = new Censore();
			fillCensoreFromResultSet(u, rs);
			list.add(u);
		}

		if (logger.isDebugEnabled()) {
			logger.debug("processSelectAll(ResultSet) - end"); //$NON-NLS-1$
		}
		return list;

	}

	/**
	 * @param u
	 * @param rs
	 */
	protected void fillCensoreFromResultSet(Censore u, ResultSet rs)
			throws Exception {
		if (logger.isDebugEnabled()) {
			logger
					.debug("fillCensoreFromResultSet(Censore, ResultSet) - start"); //$NON-NLS-1$
		}

		u.setID(rs.getInt("ID"));
		u.setWORD(rs.getString("WORD"));

		if (logger.isDebugEnabled()) {
			logger.debug("fillCensoreFromResultSet(Censore, ResultSet) - end"); //$NON-NLS-1$
		}
	}

}
